﻿using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using QRCodePass.Application.Build;
using QRCodePass.Application.Build.Dtos;
using QRCodePass.Core.Models;
using QRCodePass.Core.Repositories;
using QRCodePass.EntityFrameworkCore;
using QRCodePass.Mvc.ViewModels.Build;

namespace QRCodePass.Mvc.Controllers
{
    public class BuildController : Controller
    {
        private readonly IRepository<Build, int> _buildRepository;
        private readonly IRepository<Process, int> _processRepository;
        private readonly IBuildService _buildService;
        private readonly AppDbContext _dbcontext;

        public BuildController(
            IRepository<Build, int> buildRepository,
            IRepository<Process, int> processRepository,
            IBuildService buildService,
            AppDbContext dbcontext)
        {
            _buildRepository = buildRepository;
            _buildService = buildService;
            _dbcontext = dbcontext;
            _processRepository = processRepository;
        }


        public async Task<IActionResult> Index(GetBuildInput input)
        {
            var models = await _buildService.GetPagedBuildsList(input);
            var dto = new BuildListModel();
            if (input.Id != null)
            {       //该楼栋的流程
                var department = models.Data.FirstOrDefault(a => a.Id == input.Id.Value);
                if (department != null)
                {
                    dto.Allprocess = await _processRepository.GetAllListAsync();
                    dto.Processes = dto.Allprocess.FindAll(x => x.BuildId == department.Id);
                }
                dto.SelectedId = input.Id.Value;
            }

            dto.Build = models;
            return View(dto);
        }

        public IActionResult Create()
        {
            return View();
        }
        /// <summary>
        /// 创建楼栋方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Create(BuildCreateViewModel input)
        {
            if (ModelState.IsValid)
            {
                Build model = new Build
                {
                    Name = input.Name,
                    BuildAddress = input.BuildAddress,
                    Description = input.Description
                };
                await _buildRepository.InsertAsync(model);
                return RedirectToAction(nameof(Index));
            }
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            var model = await _buildRepository.FirstOrDefaultAsync(a => a.Id == id);

            if (model == null)
            {
                ViewBag.ErrorMessage = $"楼栋编号{id}的信息不存在，请重试。";
                return View("NotFound");
            }

            await _buildRepository.DeleteAsync(a => a.Id == id);
            return RedirectToAction(nameof(Index));
        }

        public async Task<IActionResult> Edit(int id)
        {
            var build = await _buildRepository.FirstOrDefaultAsync(a => a.Id == id);

            //var teacherList = TeachersDropDownList();
            var dto = new BuildCreateViewModel()//负责人
            {
                Name = build.Name,
                Description = build.Description,
                BuildAddress = build.BuildAddress
            };
            return View(dto);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(BuildCreateViewModel input)
        {
            if (ModelState.IsValid)
            {
                //var model = await _departmentRepository.GetAll().Include(a => a.Administrator);
                var model = await _buildRepository.GetAll()
                    .FirstOrDefaultAsync(a => a.Id == input.Id);

                if (model == null)
                {
                    ViewBag.ErrorMessage = $"系部{input.Id}的信息不存在，请重试。";
                    return View("NotFound");
                }

                model.Name = input.Name;
                model.Description = input.Description;
                model.BuildAddress = input.BuildAddress;
                //UpdateAsync方法执行SaveChangesAsync()方法时，如果检测到并发冲突则会触发DbUpdateConcurrencyException异常
                await _buildRepository.UpdateAsync(model);
                return RedirectToAction(nameof(Index));
            }
            return View(input);
        }
    }
}
